package com.example.uploadimage.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.*;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

// 定义配置类
@Configuration
public class SwaggerConfig {

	@Bean
	public Docket createRestApi() {

		//返回文档摘要信息
		return new Docket(DocumentationType.OAS_30)
				.groupName("webApi") // 分组名称
				.apiInfo(webApiInfo())
				.select()
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
				.paths(PathSelectors.any())
				.build()
				.securitySchemes(securitySchemes())
				.securityContexts(securityContexts());
	}

	private List<SecurityScheme> securitySchemes() {
		List<SecurityScheme> securitySchemes = new ArrayList<>();
//		securitySchemes.add(new ApiKey("Authorization", "Authorization", "header"));
		securitySchemes.add(new ApiKey("token", "token", "header"));
		return securitySchemes;
	}

	private List<SecurityContext> securityContexts() {
		List<SecurityContext> securityContexts = new ArrayList<>();
		securityContexts.add(SecurityContext.builder()
				.securityReferences(defaultAuth())
				.forPaths(PathSelectors.regex("^(?!auth).*$")).build());
		return securityContexts;
	}

	private List<SecurityReference> defaultAuth() {
		AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
		AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
		authorizationScopes[0] = authorizationScope;
		List<SecurityReference> securityReferences = new ArrayList<>();
		securityReferences.add(new SecurityReference("token", authorizationScopes));
		return securityReferences;
	}



	private ApiInfo webApiInfo(){
		return new ApiInfoBuilder()
				.title("点餐服务中心API文档")
				.description("本文档描述了点餐接口定义")
				.version("1.0")
				.contact(new Contact("lzy", "http://zonelzy.top",
						"2812492504@qq.com"))
				.build();
	}
}
